package lvxu.service;

import lvxu.bean.Music;
import lvxu.bean.MusicExample;
import lvxu.bean.User;
import lvxu.bean.UserExample;
import lvxu.dao.MusicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MusicListService {
    @Autowired
    MusicMapper musicMapper;

    //查询所有歌曲信息
    public List<Music> selectByAll() {
        List<Music> musicList = musicMapper.selectByExample(null);
//        System.out.println(musicList);
        return  musicList;
    }

    //通过kind查询歌曲信息
    public List<Music> selectByKind(Integer kind) {
        MusicExample musicExample = new MusicExample();
        MusicExample.Criteria criteria = musicExample.createCriteria();
        criteria.andKindEqualTo(kind);
        List<Music> musicList = musicMapper.selectByExample(musicExample);
        return musicList;
    }
    //修改收藏状态
    public boolean updateMusicLike(Integer musicid,String state){
        Music music = musicMapper.selectByPrimaryKey(musicid);
        System.out.println(1);
        Integer musiclike = music.getMusiclike();
        if (state.equals("add")){
            music.setMusiclike(musiclike+1);
        }else {
            System.out.println(2);
            music.setMusiclike(musiclike-1);
        }
        int update = musicMapper.updateByPrimaryKey(music);
        System.out.println(3 + "======" + update);
        return update != 0 ? true:false;
    }

    public Music selectByMusicId(Integer musicid) {
        Music music = musicMapper.selectByPrimaryKey(musicid);
        return music;
    }

    public boolean deleteById(Integer id) {
        int delete = musicMapper.deleteByPrimaryKey(id);
        return delete != 0 ? true : false;
    }

    public Music updateMusic(Music music) {
        MusicExample musicExample = new MusicExample();
        MusicExample.Criteria criteria = musicExample.createCriteria();
        criteria.andMusicidEqualTo(music.getMusicid());
        Music music1 = musicMapper.selectByPrimaryKey(music.getMusicid());

        if(music.getSong() == null){
            music.setSong(music1.getSong());
        }
        if(music.getSinger() == null){
            music.setSinger(music1.getSinger());
        }
        if(music.getComposer() == null){
            music.setComposer(music1.getComposer());
        }
        if(music.getHits() == null){
            music.setHits(music1.getHits());
        }
        musicMapper.updateByExample(music,musicExample);
        Music music2 = musicMapper.selectByPrimaryKey(music.getMusicid());
        System.out.println("=====================================");
        System.out.println(music2);
        System.out.println("=====================================");

        return music2;
    }

    public boolean saveMusic(Music music) {
        int insert = musicMapper.insert(music);
        return insert!=0?true:false;
    }
}
